﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Data.Quiz;
using UniMana.Utils.LoadData;
using UniMana.Utils.CusControls;
using UniMana.Lib.Quiz;
using DevExpress.XtraGrid;
using System.Linq;
using System.Data.Linq;
using DevExpress.XtraBars;

namespace UniMana.GUI.Quiz
{
    public partial class FrmApproval : DevExpress.XtraEditors.XtraForm
    {
        public DBQuiz m_data = new DBQuiz();
        public bds m_bdsDisApproval = new bds();
        public bds m_bdsApproval = new bds();


        public FrmApproval()
        {
            InitializeComponent();
            LoadData();
            xucBar1.btnSave_Click += new Controls.xucBar.ButtonClickHandler(this.btnSave_Click);
        }

        
        public void LoadData()
        {
            gluTraning_Subject.GetData(m_data.m_DCReadOnly.Training_Subjects, "Training_SubjectKey");
            lueQuiz_QuestionType.GetData(m_data.m_DCReadOnly.Quiz_QuestionTypes);
            rluQuiz_QuestionLevel_App.GetData(m_data.m_DCReadOnly.Quiz_QuestionLevels, "Quiz_QuestionLevelKey");
            rluQuiz_QuestionLevel_DisApp.GetData(m_data.m_DCReadOnly.Quiz_QuestionLevels, "Quiz_QuestionLevelKey");
            rluQuiz_SubjectTopic_App.GetData(m_data.m_DCReadOnly.Quiz_SubjectTopics, "Quiz_SubjectTopicKey");
            rluQuiz_SubjectTopic_DisApp.GetData(m_data.m_DCReadOnly.Quiz_SubjectTopics, "Quiz_SubjectTopicKey");

            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments);
        }



        public void Filter()
        {
            try
            {
                int subjectKey = int.Parse(gluTraning_Subject.EditValue.ToString());
                int qType = int.Parse(lueQuiz_QuestionType.EditValue.ToString());
                DataTable tblDisApp= this.m_data.m_DCSave.Quiz_Questions.GetListDisApproval(subjectKey, qType).ToDataTable();
                if (tblDisApp.Rows.Count > 0)
                {
                    gctDisapproval.DataSource = tblDisApp;
                }
                else
                {
                    DataTable tbl = new DataTable();
                    DataColumn cl1 = new DataColumn("Quiz_QuestionKey");
                    DataColumn cl2 = new DataColumn("QContent");
                    DataColumn cl3 = new DataColumn("Quiz_SubjectTopicKey");
                    DataColumn cl4 = new DataColumn("Quiz_QuestionLevelKey");
                    DataColumn cl5 = new DataColumn("Training_SubjectGroupKey");
                    DataColumn cl6 = new DataColumn("TeacherKey");
                    DataColumn cl7 = new DataColumn("CreatedOn");
                    tbl.Columns.AddRange(new DataColumn[] { cl1, cl2, cl3, cl4, cl5, cl6, cl7 });
                    gctDisapproval.DataSource = tbl;
                }

                
                DataTable tblApp = this.m_data.m_DCSave.Quiz_Questions.GetListApproval(subjectKey, qType).ToDataTable();
                if (tblApp.Rows.Count > 0)
                {
                    gctAprroval.DataSource = tblApp;
                }
                else
                {
                    DataTable tbl = new DataTable();
                    DataColumn cl1 = new DataColumn("Quiz_QuestionKey");
                    DataColumn cl2 = new DataColumn("QContent");
                    DataColumn cl3 = new DataColumn("Quiz_SubjectTopicKey");
                    DataColumn cl4 = new DataColumn("Quiz_QuestionLevelKey");
                    DataColumn cl5 = new DataColumn("Training_SubjectGroupKey");
                    DataColumn cl6 = new DataColumn("TeacherKey");
                    DataColumn cl7 = new DataColumn("CreatedOn");
                    tbl.Columns.AddRange(new DataColumn[] { cl1, cl2, cl3, cl4, cl5, cl6, cl7 });
                    gctAprroval.DataSource = tbl;
                }
            }
            catch { XtraMessageBox.Show("Vui lòng kiểm tra lại điều kiện lọc", "Ngân hàng đề thi-Lỗi",MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } 

        }

        private void btnApproval_Click(object sender, EventArgs e)
        {
            MoveItems(gctAprroval, grvDisapproval);
        }

        private void btnDisApproval_Click(object sender, EventArgs e)
        {
            MoveItems(gctDisapproval, grvApproval);
        }


        /// <summary>
        /// Di chuyển các ô qua lại giữa các lưới
        /// </summary>
        /// <param name="_grcTarget">GridControl đích</param>
        /// <param name="_grvSource">GridView nguồn</param>
        public void MoveItems(GridControl _grcTarget, DevExpress.XtraGrid.Views.Grid.GridView _grvSource)
        {
            DataTable tbl = _grcTarget.DataSource as DataTable;
            foreach (int i in _grvSource.GetSelectedRows())
            {
                if (_grvSource.IsGroupRow(i))
                {
                    XtraMessageBox.Show("Vui lòng không chọn nhóm!","Ngân hàng đề thi-Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
            foreach (int i in _grvSource.GetSelectedRows())
            {
                //try
                //{
                    DataRow newRow = tbl.NewRow();
                    newRow["Quiz_QuestionKey"] = _grvSource.GetRowCellValue(i, "Quiz_QuestionKey");
                    newRow["QContent"] = _grvSource.GetRowCellValue(i, "QContent");
                    newRow["Quiz_SubjectTopicKey"] = _grvSource.GetRowCellValue(i, "Quiz_SubjectTopicKey");
                    newRow["Quiz_QuestionLevelKey"] = _grvSource.GetRowCellValue(i, "Quiz_QuestionLevelKey");
                    newRow["TeacherKey"] = _grvSource.GetRowCellValue(i, "TeacherKey");
                    newRow["CreatedOn"] = _grvSource.GetRowCellValue(i, "CreatedOn");
                    tbl.Rows.Add(newRow);
                //}
                //catch { }
            }
            _grvSource.DeleteSelectedRows();
        }

        //Nút xem được click
        private void btnView_Click(object sender, EventArgs e)
        {
            Filter();
        }

        private void btnSave_Click(object sender, ItemClickEventArgs e)
        {

            int numApps = 0;
            //Cập nhật Status của lưới bên phải là đã duyệt (=true)
            for (int i = 0; i < (gctAprroval.DataSource as DataTable).Rows.Count; i++ )
            {
                try
                {
                    string qKey = (gctAprroval.DataSource as DataTable).Rows[i]["Quiz_QuestionKey"].ToString();
                    Quiz_QuestionInfo info = new Quiz_QuestionInfo(qKey);
                    info.Properties.Status = true;
                    info.Save();
                    numApps++;
                }
                catch { }
            }
            //Cập nhật Status của lưới bên trái là chưa duyệt (=false)
            for (int i = 0; i < (gctDisapproval.DataSource as DataTable).Rows.Count; i++)
            {
                try
                {
                    string qKey = (gctDisapproval.DataSource as DataTable).Rows[i]["Quiz_QuestionKey"].ToString();
                    Quiz_QuestionInfo info = new Quiz_QuestionInfo(qKey);
                    info.Properties.Status = false;
                    info.Save();
                }
                catch { }
            }
            XtraMessageBox.Show("Số câu đã duyệt trong ngân hàng đề :" + numApps, "Ngân hàng đề thi", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void gctDisapproval_DoubleClick(object sender, EventArgs e)
        {
            this.ShowQuestionDetail(grvDisapproval);
        }

        private void gctAprroval_DoubleClick(object sender, EventArgs e)
        {
            this.ShowQuestionDetail(grvApproval);
        }

        public void ShowQuestionDetail(DevExpress.XtraGrid.Views.Grid.GridView view)
        {
            try
            {
                string quizKey = view.GetFocusedRowCellValue("Quiz_QuestionKey").ToString();
                Quiz_QuestionInfo questionInfo = new Quiz_QuestionInfo(quizKey);
                Quiz_AnsewerTextInfo ansTextInfo = new Quiz_AnsewerTextInfo(quizKey);
                FrmQuestionDetail frmDetail = new FrmQuestionDetail();
                frmDetail.m_QuestionInfo = questionInfo;
                frmDetail.m_AswTextInfo = ansTextInfo;

                frmDetail.ShowDialog();
            }
            catch { XtraMessageBox.Show("Không thể xem câu hỏi", "Ngân hàng đề thi - Lỗi"); }
        }






    }
}